import QtQuick 1.1
import QtMobility.location 1.2

import com.nokia.meego 1.0

Sheet {
    id: addressPicker
    acceptButtonText: qsTr("Save")
    rejectButtonText: qsTr("Cancel")

    function handleGPS() {
        if (visible && Qt.application.active) {
            console.log("gps start")
            positionSource.start()
        } else {
            console.log("gps stop")
            positionSource.stop()
        }

    }

    onVisibleChanged: handleGPS()
    Connections {
        target: Qt.application
        onActiveChanged: handleGPS()
    }


    content: Rectangle {
        anchors.fill: parent

        //anchors.fill: parent
        PinchArea {
           id: pincharea

           property double __oldZoom

           anchors.fill: parent

           function calcZoomDelta(zoom, percent) {
              return zoom + Math.log(percent)/Math.log(2)
           }

           onPinchStarted: {
              __oldZoom = map.zoomLevel
           }

           onPinchUpdated: {
              map.zoomLevel = calcZoomDelta(__oldZoom, pinch.scale)
           }

           onPinchFinished: {
              map.zoomLevel = calcZoomDelta(__oldZoom, pinch.scale)
           }
        }
        MouseArea {
           id: mousearea

           property bool __isPanning: false
           property int __lastX: -1
           property int __lastY: -1

           anchors.fill: parent

           onPressed: {
              __isPanning = true
              __lastX = mouse.x
              __lastY = mouse.y
           }

           onReleased: {
              __isPanning = false
           }

           onPositionChanged: {
              if (__isPanning) {
                  var dx = mouse.x - __lastX
                  var dy = mouse.y - __lastY
                  map.pan(-dx, -dy)
                  map.center = map.toCoordinate(Qt.point(map.width/2,map.height/2))
                  __lastX = mouse.x
                  __lastY = mouse.y
              }
           }

           onCanceled: {
              __isPanning = false;
           }
        }
        PositionSource {
            id: positionSource
            updateInterval: 1000
        }
        Map {
            id: map
            anchors.fill: parent
            plugin : Plugin {
                        name : "nokia";
                        parameters: [
                            PluginParameter { name: "app_id"; value: "YalGd1OFQGLbZPIitw8j" },
                            PluginParameter { name: "token"; value: "ENBAbE3oVEzm0D9%2BCskyFw%3D%3D" }
                       ]
                    }
            zoomLevel: 17
            center: positionSource.position.coordinate

            MapImage {
                id: markPin
                source:  "../../images/icon-m-common-location-selected.png"
                coordinate: positionSource.position.coordinate
//                offset.x: -64
//                offset.y: -113
                offset.x: -72
                offset.y: -144
                MapMouseArea {
                    property bool __isPanning: false
                    anchors.fill: markPin
                    onPressed: { __isPanning = true }
                    onReleased: {
                        __isPanning = false
                        rosYama.geocode(Qt.point(markPin.coordinate.latitude,markPin.coordinate.longitude))
                    }
                    onPositionChanged: {
                       if (__isPanning) {
                          markPin.coordinate = map.toCoordinate(Qt.point(mouse.x,mouse.y))
                       }
                    }
                }
            }
            MapCircle {
                id: myPosition
                color: "#80FF0000"
                border.color: "#80FFFFFF"
                radius: positionSource.position.horizontalAccuracy
                center: positionSource.position.coordinate
            }
        }

        TextArea {
            id: addressField
            placeholderText: qsTr("Drag the pin to the nearest house")
            anchors {
                left: parent.left
                right: parent.right
                bottom: parent.bottom
                margins: UiConstants.DefaultMargin
            }
        }
    }
    Component.onDestruction: positionSource.stop();

    Connections {
        target: Qt.application
        onActiveChanged: {
            if (Qt.application.active)
                positionSource.start();
            else
                positionSource.stop();
        }
    }

    Connections {
        target: rosYama
        onGeocodeResult: {
            addressField.text = result;
        }
    }

    onAccepted: {
        addressArea.text = addressField.text
        addDefect.holeLatitude = markPin.coordinate.latitude
        addDefect.holeLongitude = markPin.coordinate.longitude
    }
    onRejected: {
        markPin.coordinate = positionSource.position.coordinate;
        map.center = positionSource.position.coordinate;
    }
}
